package com.huifaapp.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.AlreadyPayActivity;
import com.huifaapp.mall.bean.PendingDeliveryBean;

import java.util.List;

/**
 * Created by mac on 2017/12/8.
 */

public class PendingDeliveryAdapter extends RecyclerView.Adapter<PendingDeliveryAdapter.MyViewHolder> {
    private Context context;
    private List<PendingDeliveryBean.OrderListBean> list;

    public PendingDeliveryAdapter(Context context, List<PendingDeliveryBean.OrderListBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public PendingDeliveryAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.over_rv_item, null);
        return new PendingDeliveryAdapter.MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(PendingDeliveryAdapter.MyViewHolder holder, final int position) {
        final PendingDeliveryBean.OrderListBean orderListBean = list.get(position);   //获取当前条目的数据
        List<PendingDeliveryBean.OrderListBean.DataBean> data = orderListBean.getData();   //获取到内层的数据集合
        PendingDeliveryAdaptertwo pendingDeliveryAdaptertwo = new PendingDeliveryAdaptertwo(context,data);//创建内层的适配器


        pendingDeliveryAdaptertwo.setOnItemClickListener(new PendpayAdaptertwo.OnItemClickListenertwo() {
            @Override
            public void OnItem(View view, int position) {
                Intent intent = new Intent(context, AlreadyPayActivity.class);
                intent.putExtra("orderNo",orderListBean.getOrderNo());
                Log.e("待发货order",orderListBean.getOrderNo()+"");
                context.startActivity(intent);
            }
        });
        holder.deliveryrecy.setAdapter(pendingDeliveryAdaptertwo);   //绑定内层的适配器
        holder.sum_count.setText("共计" + orderListBean.getTotalcount() + "件商品");   //商品的总个数
        holder.sum_money.setText("合计: ¥" + orderListBean.getTotalprice());   //商品的总价
        holder.yunfei.setText("(含运费¥ " + orderListBean.getExpressFee() + ")");   //商品的运费
        holder.ddnum.setText(orderListBean.getOrderNo()+"");

//        if (onItemClickListener != null) {
//            //联系买家
//            holder.contactSeller.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    onItemClickListener.onContactSeller(position);
//                }
//            });
//
//            //取消订单
//            holder.cancelOrder.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    onItemClickListener.onCancelOrder(position);
//                }
//            });
//
//            //付款
//            holder.payment.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    onItemClickListener.onPayment(position);
//                }
//            });
//        }

        if (onItemClickListener!=null){
            holder.xiangqing.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.onSmall(position);

                }
            });
        }



    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        RecyclerView deliveryrecy;
        TextView yunfei, sum_money, sum_count,ddnum;
        Button xiangqing;
//        Button contactSeller, cancelOrder, payment;

        public MyViewHolder(View itemView) {
            super(itemView);
            deliveryrecy = (RecyclerView) itemView.findViewById(R.id.over_rv_item_recy);
            deliveryrecy.setLayoutManager(new LinearLayoutManager(context));
            yunfei = (TextView) itemView.findViewById(R.id.overhang_hyf);
            sum_money = (TextView) itemView.findViewById(R.id.overhang_hj);
            sum_count = (TextView) itemView.findViewById(R.id.overhaang_gj);
            ddnum = (TextView) itemView.findViewById(R.id.over_rv_itemddnum);
            xiangqing = (Button) itemView.findViewById(R.id.rv_item_xiangqing);


//            recy111 = (RecyclerView) itemView.findViewById(R.id.recy111);
//            recy111.setLayoutManager(new LinearLayoutManager(context));
//
//            yunfei = (TextView) itemView.findViewById(R.id.yunfei);
//            sum_money = (TextView) itemView.findViewById(R.id.sum_money);
//            sum_count = (TextView) itemView.findViewById(R.id.sum_count);
//
//            contactSeller = (Button) itemView.findViewById(R.id.contactSeller);
//            cancelOrder = (Button) itemView.findViewById(R.id.cancelOrder);
//            payment = (Button) itemView.findViewById(R.id.payment);


        }
    }

    //监听事件接口
    public interface OnItemClickListener {

        void onSmall(int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}